package com.bjdd.jdy.ui.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.design.widget.TabLayout;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjdd.jdy.App;
import com.bjdd.jdy.R;
import com.bjdd.jdy.baidu.MyService;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;

import butterknife.Bind;
import butterknife.OnClick;


public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginContract.View {


    @Bind(R.id.et_login_name)
    EditText etLoginName;
    @Bind(R.id.et_login_pwd)
    EditText etLoginPwd;
    @Bind(R.id.tv_login_register)
    TextView tvLoginRegister;
    @Bind(R.id.btn_login)
    AppCompatButton btnLogin;
    @Bind(R.id.tv_login_forget)
    TextView tvForget;
    @Bind(R.id.tl_login_tab)
    TabLayout tabLayout;
    @Bind(R.id.til_login_pwdlogin)
    LinearLayout pwdLogin;
    @Bind(R.id.ll_login_codelogin)
    LinearLayout codeLogin;
    @Bind(R.id.et_login_code)
    EditText etCode;
    @Bind(R.id.btn_login_getcode)
    Button btnCode;
    private CountDownTimer timer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.primary),0);
        tabLayout.addTab(tabLayout.newTab().setText("快捷登陆"));
        tabLayout.addTab(tabLayout.newTab().setText("密码登陆"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0){
                    pwdLogin.setVisibility(View.GONE);
                    codeLogin.setVisibility(View.VISIBLE);
                }else if (tab.getPosition() == 1){
                    pwdLogin.setVisibility(View.VISIBLE);
                    codeLogin.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick({R.id.tv_login_register, R.id.btn_login,R.id.tv_login_forget,R.id.btn_login_getcode})
    public void clickEvent(View view) {
        switch (view.getId()) {
            case R.id.tv_login_register:
                //点击注册
                JumpUtil.toRegister(context,"用户注册",0);
                break;
            case R.id.btn_login:
                //点击登录

                if (TextUtils.isEmpty(etLoginName.getText().toString())) {
                    T.show("请填写手机号码");
                    return;
                }
                if (tabLayout.getSelectedTabPosition() == 1 ){
                    if (TextUtils.isEmpty(etLoginPwd.getText().toString())){
                        T.show("密码不能为空");
                        return;
                    }
                    presenter.login(etLoginName.getText().toString(), etLoginPwd.getText().toString());
                }
                if (tabLayout.getSelectedTabPosition() == 0 ){
                    if (TextUtils.isEmpty(etCode.getText().toString())){
                        T.show("验证码不能为空");
                        return;
                    }
                    presenter.codeLogin(etLoginName.getText().toString(),etCode.getText().toString());
                }

                break;
            case R.id.tv_login_forget:
                JumpUtil.toRegister(context,"找回密码",1);
                break;
            case R.id.btn_login_getcode:
                String mobile = etLoginName.getText().toString();
                if (TextUtils.isEmpty(mobile) || TextUtils.equals(mobile,"")){
                    T.show("请先输入手机号码");
                    return;
                }
                long timeAll = 60*1000;
                long timeSpace = 1000;
                btnCode.setClickable(false);
                btnCode.setBackground(getResources().getDrawable(R.drawable.unclickable_rounded));
                presenter.getCode(mobile);
                timer = new CountDownTimer(timeAll,timeSpace) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        btnCode.setText("("+millisUntilFinished/timeSpace+")重新获取");
                    }

                    @Override
                    public void onFinish() {
                        btnCode.setClickable(true);
                        btnCode.setText("发送验证码");
                        btnCode.setBackground(getResources().getDrawable(R.color.primary));
                    }
                };
                timer.start();
                break;
            default:
                break;
        }
    }

    @Override
    public void loginSuccess(User user) {
        T.show("登录成功");
        if (timer != null){
            timer.onFinish();
            timer.cancel();
            timer = null;
        }
        JumpUtil.toMain(context);
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //App.getApp().exit();
    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                T.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                App.getApp().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
